#include "autocamform.h"
#include "ui_autocamform.h"
#include"data.h"
#include<QMessageBox>
AutoCamForm::AutoCamForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AutoCamForm)
{
    ui->setupUi(this);
    ui->checkBox_op0->setEnabled(false);
    timer=new QTimer(this);timer->setInterval(200);
    timer->start();
    connect(timer,&QTimer::timeout,this,&AutoCamForm::timerOut);
    connect(this,&AutoCamForm::runFinishSignal,this,[&](){
        ui->pushButton_run->setText(tr("启动"));
    });

    ui->radioButton->setChecked(true);
}

AutoCamForm::~AutoCamForm()
{
    isStart=false;
    delete ui;
}

void AutoCamForm::on_tableWidget_pos_itemChanged(QTableWidgetItem *item)
{
    QString Pattern("( -? [1-9][0-9]+)|( -? [0-9])|( -? [1-9]\\d+\\.\\d+)| ( -? [0-9]\\. \\d+)");
    QRegExp reg(Pattern);

    QString str=item->text();

    //完全匹配
    if(reg.exactMatch(str)){


    }else{
        item->setText("0");
    }
}


void AutoCamForm::on_pushButton_run_clicked()
{

    if(!isStart && Data::CardIsConnect()){
        ui->pushButton_run->setText(tr("停止"));
        isStart=true;
        PMC_HANDLE *handle=&Data::handle;
        for(int i=0;i<2;++i){
            int enable=0;
            PMC_GetAxisEnable(*handle,i,&enable);
            if(enable==0){
                QMessageBox::about(NULL,"Warn","请对AXIS0,1进行使能！！！");
            }
        }

        //AXIS0是主轴，AXIS1是凸轮跟随轴
        PMC_SetAtype(*handle,0,1);
        PMC_SetUnits(*handle,0,100);
        PMC_SetSpeed(*handle,0,100);
        PMC_SetAccel(*handle,0,1000);
        PMC_SetDecel(*handle,0,1000);
        PMC_SetMerge(*handle,0,1);

        PMC_SetAtype(*handle,1,1);
        PMC_SetUnits(*handle,1,100);
        PMC_SetSpeed(*handle,1,100);
        PMC_SetAccel(*handle,1,1000);
        PMC_SetDecel(*handle,1,1000);
        PMC_SetMerge(*handle,1,1);

        if(ui->radioButton->isChecked()){   //位置模式
           float distance= ui->tableWidget_pos->item(0,0)->text().toFloat();
           float linkDist= ui->tableWidget_pos->item(0,1)->text().toFloat();
           float accDist= ui->tableWidget_pos->item(0,2)->text().toFloat();
           float decDist= ui->tableWidget_pos->item(0,3)->text().toFloat();
           PMC_Movelink(*handle,1,distance,linkDist,accDist,decDist,0,0,0);
           PMC_MoveOp(*handle,1,0,1);
           distance= ui->tableWidget_pos->item(1,0)->text().toFloat();
           linkDist= ui->tableWidget_pos->item(1,1)->text().toFloat();
           accDist= ui->tableWidget_pos->item(1,2)->text().toFloat();
           decDist= ui->tableWidget_pos->item(1,3)->text().toFloat();
           PMC_Movelink(*handle,1,distance,linkDist,accDist,decDist,0,0,0);
           PMC_MoveOp(*handle,1,0,0);
           distance= ui->tableWidget_pos->item(1,0)->text().toFloat();
           linkDist= ui->tableWidget_pos->item(1,1)->text().toFloat();
           accDist= ui->tableWidget_pos->item(1,2)->text().toFloat();
           decDist= ui->tableWidget_pos->item(1,3)->text().toFloat();
           PMC_Movelink(*handle,1,distance,linkDist,accDist,decDist,0,0,0);

        }else if(ui->radioButton_2->isChecked()){   //速度模式
            float distance= ui->tableWidget_spd->item(0,0)->text().toFloat();
            float linkDist= ui->tableWidget_spd->item(0,1)->text().toFloat();
            float accRatio= ui->tableWidget_spd->item(0,2)->text().toFloat();
            float decRatio= ui->tableWidget_spd->item(0,3)->text().toFloat();
            PMC_Moveslink(*handle,1,distance,linkDist,accRatio,decRatio,0,0,0);
            PMC_MoveOp(*handle,1,0,1);
            distance= ui->tableWidget_spd->item(1,0)->text().toFloat();
            linkDist= ui->tableWidget_spd->item(1,1)->text().toFloat();
            accRatio= ui->tableWidget_spd->item(1,2)->text().toFloat();
            decRatio= ui->tableWidget_spd->item(1,3)->text().toFloat();
            PMC_Moveslink(*handle,1,distance,linkDist,accRatio,decRatio,0,0,0);
            PMC_MoveOp(*handle,1,0,0);
            distance= ui->tableWidget_spd->item(1,0)->text().toFloat();
            linkDist= ui->tableWidget_spd->item(1,1)->text().toFloat();
            accRatio= ui->tableWidget_spd->item(1,2)->text().toFloat();
            decRatio= ui->tableWidget_spd->item(1,3)->text().toFloat();
            PMC_Moveslink(*handle,1,distance,linkDist,accRatio,decRatio,0,0,0);
        }
        PMC_Single_Move(*handle,0,ui->doubleSpinBox_pos->value());
        std::thread thr([&](){
            int idle0=0;
            int ret=0;
            while (ret==0 && idle0==0 && isStart) {
                ret=PMC_GetIfIdle(Data::handle,0,&idle0);
                QThread::msleep(10);
            }
            isStart=false;
            emit runFinishSignal();
        });
        thr.detach();


    }else{
        ui->pushButton_run->setText(tr("启动"));
        isStart=false;
    }
}


void AutoCamForm::timerOut(){
    if(Data::CardIsConnect()){
        float pos0,pos1;
        uint opState=0;
        PMC_GetDpos(Data::handle,0,&pos0);
        PMC_GetDpos(Data::handle,0,&pos1);
        PMC_GetOp(Data::handle,0,&opState);

        ui->label_0pos->setText(QString::number(pos0));
        ui->label_1pos->setText(QString::number(pos1));
        ui->checkBox_op0->setChecked(opState);

    }
}
